<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>悬赏详情-思多客</title>
    <link href="http://cdn.bootcss.com/normalize/7.0.0/normalize.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/public/css/m/public.css">
    <link rel="stylesheet" href="/public/css/m/reward-detail.css">
</head>

<body>
    <div class="page-content">
        <div class="new-header flex">
            <div class="back-box">
                <img src="/public/images/m/back.png" alt="" class="left-back-icon">
            </div>
            <span class="header-title flex-child-box">悬赏详情</span>
        </div>
        <div class="reward-content">
            <div class="qs-box-list">
                <div id="J_reward"></div>
                <div class="comment-box clearfix">
                    <div class="fl">
                        <img src="/public/images/m/dec.png" alt="" class="showdec"> 查看评论
                    </div>
                    <!-- <div class="fr ohter-dec">补充说明</div> -->
                </div>
                <ul class="list-box-ul" id="J_reward_comment"></ul>
            </div>
            <div class="clearfix all-reward">
                <h3 class="fl">所有竞赏</h3>
                <!-- <span class="fr list-right-tips">若3日内不确定打赏，赏金将自动发给点赞数最高的老师</span> -->
            </div>
            <div class="qs-box-list">
                <ul class="list-box-ul" id="J_rank"></ul>
            </div>
        </div>
        <div id="footer"></div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/public/js/m/fontResize.js"></script>
    <script src="/public/lib/layer/layui.js"></script>
    <script src="/public/js/G.js"></script>
    <script src="/public/js/user.js"></script>
    <script src="/public/js/reward.js"></script>
    <script src="/public/js/rewardComment.js"></script>
    <script src="/public/js/m/public.js"></script>
    <script id="J_my_reward_tpl" type="text/html">
        <div class="list-title clearfix">
            <h4 class="list-big-title fl">{{d.title}}</h4>
            <div class="right-small-txt fr">
                <p class="reward-time">{{new Date(d.createTime).format('MM-DD HH:ii')}}<br>{{formatRewardType(d.type)}}</p>
            </div>
        </div>
        <div class="list-user-msg flex">
            <div class="reward-header"><img src="/{{G.defaultAvator}}" data-lazyload-img="{{G.uploadurl}}/avator/{{d.userId}}.png" alt=""></div>
            <span class="reward-username">{{d.createUser.username}}</span>
            <span class="reward-money flex-3">悬赏额：<b>{{d.amountPay}}</b></span>
        </div>
        <div class="dec-box">{{d.content}}</div>
        <div class="append-describe"></div>
    </script>
    <script id="J_reward_comment_tpl" type="text/html">
        {{# d.list.forEach(function(val,index){ }}
        <li class="comment-list clearfix">
            <div class="list-user-msg flex">
                <div class="reward-header"><img src="/public/images/m/t-icon.png" alt="" class=""></div>
                <span class="reward-username">{{val.createUserName}}</span>
                <span class="reward-time flex-3">{{new Date(val.createTime).format('YYYY-MM-DD HH:ii:ss')}}</span>
            </div>
            <p class="showdec-txt">{{# if(val.replyUserName){ }}回复{{val.replyUserName}}:{{# }; }} {{val.commentContent}}</p>
        </li>
        {{# }); }}
    </script>
    <script id="J_rank_tpl" type="text/html">
        {{# d.forEach(function(val,index){ }}
        <li class="comment-list clearfix">
                <div class="list-user-msg">
                    <div class="reward-header inline-block">
                        <img src="{{G.defaultAvator}}" data-lazyload-img="{{G.uploadurl}}/avator/{{val.createUserId}}.png" alt="">
                    </div>
                    <div class="inline-block">
                        <h4 class="report-name">{{val.aliasName?val.aliasName:val.name}}</h4>
                        <div class="reward-time">2017-08-08<span class="ups"><img src="/public/images/m/good.png" alt="">{{val.thumbUpNumber}}点赞数</span></div>
                    </div>
                    
                {{# if(val.win){ }}
                    <div class="inline-block show-reward is-win">已打赏</div>
                {{# }else{ }}
                    <div class="inline-block show-reward" id="setFinish" data-rewardid="{{val.rewardId}}" data-rewardpkid="{{val.rewardPKId}}">打赏TA</div>
                {{# }; }}
                <a href="{{G.apiurl}}api/rewardPK/{{val.rewardId}}/{{val.rewardPKId}}/download?token={{G.getCookie('jwt')}}" class="download">
                    <div class="inline-block show-reward" id="J_download_rank">查看</div>
                </a>
            </div>
        </li>
        {{# }); }}
    </script>

    <script>

    layui.use('laytpl', function() {
        var laytpl = layui.laytpl;

        var rewardId = G.getQuery('rewardId');
        var localUserId = G.getCookie('localUserId') || G.getQuery('localUserId');
        var type = G.getCookie('sdk_user_type');
        var userId = G.getCookie('sdk_user_id');
        var rewardParams = {
            rewardId: rewardId
        }
        if(!userId){
            alert('请登录');
            location.href = '/m/login.html?from=reward-detail.html&rewardId='+rewardId+'&localUserId='+localUserId;
            return;
        }
        if(localUserId != userId){
            alert('用户ID错误')
            location.href = '/m/reward.html';
            return;
        }
        if(!rewardId){
            alert('无效的悬赏ID');
            location.href = '/m/reward.html';
            return;
        }
        loading.open()
        //获取用户的悬赏详情    
        reward.getMyReward(rewardParams, function(data) {
            console.log(data.list[0])
            laytpl($('#J_my_reward_tpl').html()).render(data.list[0], function(html) {
                $('#J_reward').html(html)
                lazyload()
                loading.close()
            })
        })

        var rewardReplyParams = {
            count: 0,
            pageId: 1,
            pageSize: 5
        }

        loading.open()
        //获取悬赏评论列表
        rewardComment.getListById(rewardId, rewardReplyParams, function(data) {
            rewardReplyParams.count = data.count;
            console.log(data)
            if(data.count == 0){
                $('#J_reward_comment').html('<li class="center"><img style="width:3.13rem;" src="/public/images/m/no_content.png" alt="暂无内容"></li>')
                loading.close()
                return;
            }
            laytpl($('#J_reward_comment_tpl').html()).render(data, function(html) {
                $('#J_reward_comment').html(html)
                lazyload()
                loading.close()
            })
            getReplenishmentList(rewardId)
        })

        //获取追加描述列表
        function getReplenishmentList(rewardId){
            loading.open()
            rewardComment.getReplenishmentList(rewardId,{},function(data){
                var html = '';
                data.list.forEach(function(val,index){
                    html += '<p>追加描述：' + val.content + '</p>';
                })
                $('.append-describe').append(html)
                loading.close()
            })
        }
        loading.open()
        reward.getPK(rewardId,{},function(data){
            console.log(data)
            if(data.count == 0){
                $('#J_rank').html('<li class="center"><img style="width:3.13rem;" src="/public/images/m/no_content.png" alt="暂无内容"></li>')
                loading.close()
                return;
            }
            laytpl($('#J_rank_tpl').html()).render(data.list, function(html) {
                $('#J_rank').html(html)
            })
            lazyload()
            loading.close()
        })
    })

    //格式化悬赏类型
    function formatRewardType(type) {
        switch (type) {
            case 'A':
                return '个股诊断';
            case 'B':
                return '季度策略';
            case 'C':
                return '解套策略';
            case 'D':
                return '年度策略';
        }
    }

    //设置为最佳
    $(document).on('click','#setFinish',function(){
        var rewardId = $(this).data('rewardid');
        var rewardPKId = $(this).data('rewardpkid');
        console.log(rewardId,rewardPKId)
        loading.open();
        reward.finish(rewardId,rewardPKId,{},function(){
            loading.close();
            alert('打赏成功')
            location.reload()
        },function(err){
            loading.close();
            alert(err)
        })
    })

    </script>
</body>

</html>